<?php
class Macys_Base_Session {

	CONST APIPROXY = 'mashery';
	CONST APISERVICES = 'services';
	CONST KEY = 'key';
	CONST MCOMSITE = 'mcom';
	CONST BCOMSITE = 'bcom';

	private $log;
	private $config;
	private $mode;
	private $valid_modes = array (self::APIPROXY, self::APISERVICES);
	private $site;
	private $valid_sites = array(self::MCOMSITE, self::BCOMSITE);
	/**
	 * constructor for log
	 */
	public function __construct(){
		$this->log = Zend_Registry::get('log');
		//$this->config = Zend_Registry::get('MACYS_CONFIG');
		//$this->setSite($this->config['site']);
		//$this->setMode($this->config['mode']);
	}

	/**
	 * User gets the Services Gateway url
	 * Used by OAuth Server & Direct Api Call
	 * Will have restricted access
	 */
/*	public function getServicesUri() {
		return $this->config[$this->site][self::APISERVICES]['url'];
	}
*/
	/**
	 * Provides the user with Client Facing Url for Services
	 * This will be Mashery Url mostly
	 */ /*
	public function getApiUri() {
		return $this->config[$this->site][self::APIPROXY]['url'];
	}
	
	public function getUri() {
		if($this->mode == self::APIPROXY)
		return $this->getApiUri();
		else
		return $this->getServicesUri();
	}

	public function getApiKey($serviceName) {
		return $this->config[$this->site][$this->mode][self::KEY][$serviceName];
	}
	
	

	public function setMode($mode) {
		if(in_array($mode, $this->valid_modes)) {
			$this->mode = $mode;
		}
		else {
			throw new Macys_Base_Exception('Invalid Mode Please set Valid Mode');
		}
	}
	
	public function getSite() {
		return $this->site;
	}
	
	public function setSite($site) {
		if(in_array($site, $this->valid_sites)) {
			$this->site = $site;
		}
		else {
			throw new Macys_Base_Exception('Invalid SITE Please set Valid Site');
		}
	}
*/
	/**
	 * Provides the current configuration
	 * of OAuth Details the Client can use
	 */ /*
	public function getOAuthDetails() {
		return array ('client_id' => $this->config[$this->site][self::APIPROXY]['oauth']['client_id'],
						'secret' => $this->config[$this->site][self::APIPROXY]['oauth']['secret'],
						'mashery_uri' => $this->getApiUri(),
						'auth_uri' => $this->getServicesUri()
		);
	}


	public function getProxy(){
		$proxy = array(
		"host" => "mw000xnchegrn01",
		"port" => "80",
		"username" => "yc30v1m",
		"password" => "sep2011"
		);
		return $proxy;
	}
	*/
	/**
	 * retrive the session data from Zend_Session_Namespace
	 * If Namespace is not throws Macys_Base_Exception
	 * @param string $namespace
	 * @param string $variable
	 */
	public function getSessionData($namespace, $key) {
		if (Zend_Session::namespaceIsset($namespace)) {
			$out = new Zend_Session_Namespace($namespace);
			if (isset($out->$key)){
				$this->log->debug('SUCCESS Data Request: Namespace = ' . $namespace . ', keyValue : ' . $key . ' = '. $out->$key );
				return $out->$key;
			}
			else {
				$this->log->debug('NOTAVAILABLE Data Request: Namespace = ' . $namespace . ', key = ' . $key);
				return null;
			}

		}
		else {
			throw new Macys_Base_Exception('Session data not set for the Variable : ' . '2000');
		}
	}

	/**
	 * Sets Session Data
	 * @param string $namespace
	 * @param string $key
	 * @param mixed $data
	 */
	public function setSessionData($namespace,$key, $data) {
		$in = new Zend_Session_Namespace($namespace);
		$in->$key = $data;
		return $this;
	}

	/**
	 * gets /sets session Data
	 * Wrapper for Set & Get Functions
	 * @param string $namespace
	 * @param string $key
	 * @param ANY $data
	 */
	public function sessionData($namespace, $key, $data=null) {
		if(!is_null($data)) {
			$this->log->debug('SET Session Data, Namespace = ' . $namespace . ', Key = ' . $key . ', Data = ' . $data);
			return $this->setSessionData($namespace, $key, $data);
		}
		else
		{
			$this->log->debug('GET Session Data, Namespace = ' . $namespace . ', Key = ' . $key);
			return $this->getSessionData($namespace, $key);
		}
	}

	/**
	 * Unset the session
	 */
	public function sessionUnset($namespace) {
		Zend_Session::namespaceUnset($namespace);

	}
}